from datetime import datetime

from django.contrib.auth import get_user_model
from django.db import models


User = get_user_model()


class Tag(models.Model):
    """
    标签
    """
    user = models.ForeignKey(User, null=False, blank=False, verbose_name="添加标签的用户", on_delete=models.CASCADE,
                             help_text='用户名')
    name = models.CharField(max_length=30, null=False, blank=False, verbose_name="标签名", help_text='标签名')
    tag_hash = models.CharField(max_length=32, null=False, blank=False, verbose_name='标签hash', help_text='标签ID')
    add_time = models.DateTimeField(null=False, blank=False, default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name = "标签"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Problem(models.Model):
    """
    问题
    """
    title = models.CharField(max_length=200, null=False, blank=False, verbose_name="问题标题", help_text='问题标题')
    problem_hash = models.CharField(max_length=32, null=False, blank=False, verbose_name='问题hash', help_text='问题ID')
    user = models.ForeignKey(User, null=False, blank=False, verbose_name="发布用户", on_delete=models.CASCADE,
                             help_text='用户')
    brief = models.CharField(max_length=200, null=True, blank=True
                             , verbose_name="问题简介", help_text='问题简介')
    detail = models.TextField(null=True, blank=True, verbose_name="问题详细描述", help_text='问题详细描述')
    answer_count = models.IntegerField(null=0, blank=0, default=0, verbose_name='回答数', help_text='问题回答数')
    share_count = models.IntegerField(null=0, blank=0, default=0, verbose_name='分享数', help_text='分享数')
    follow_count = models.IntegerField(null=0, blank=0, default=0, verbose_name='关注数', help_text='关注数')
    add_time = models.DateTimeField(null=False, blank=False, default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name = "问题"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


class Answer(models.Model):
    """
    对问题的回答
    """
    problem = models.ForeignKey(Problem, null=False, blank=False, verbose_name="对应的问题",
                                on_delete=models.CASCADE, help_text='答案对应的问题')
    user = models.ForeignKey(User, null=True, blank=True, verbose_name="用户", on_delete=models.CASCADE, help_text='用户')
    content = models.TextField(null=False, blank=False, verbose_name='回答的内容', help_text='回答的内容')
    agree_count = models.IntegerField(null=0, blank=0, default=0, verbose_name='赞同数', help_text='赞同数')
    opposite_count = models.IntegerField(null=0, blank=0, default=0, verbose_name='反对数', help_text='反对数')
    fav_count = models.IntegerField(null=0, blank=0, default=0, verbose_name='收藏数', help_text='收藏数')
    share_count = models.IntegerField(null=0, blank=0, default=0, verbose_name='分享数', help_text='分享数')
    comment_count = models.IntegerField(null=0, blank=0, default=0, verbose_name='评论数', help_text='评论数')

    answer_hash = models.CharField(max_length=32, blank=False, null=False, verbose_name='回答的hash', help_text='回答ID')
    add_time = models.DateTimeField(null=False, blank=False, default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name = "回答"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.content


class Task(models.Model):
    """
    任务
    """
    pass


class Comment(models.Model):
    """
    对回答进行评论
    """
    answer = models.ForeignKey(Answer, null=False, blank=False, verbose_name="回答", on_delete=models.CASCADE,
                               related_name='comment', help_text='评论对应的回答')
    user = models.ForeignKey(User, verbose_name="用户", null=True, blank=True, on_delete=models.CASCADE, help_text='用户')
    comment = models.CharField(max_length=500, verbose_name="评论", null=False, blank=False, help_text='评论')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')

    class Meta:
        verbose_name = "评论"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.comment
